package edu.ua.bmr.claxml;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * ClaXMLSet should provide a way to store strings extracted from
 * XML formatted as such:
 * 
 * <responsetype>
 * <objecttype>
 * 	<field>Value</field>
 * 	<field2>Value</field2>
 * </objecttype>
 * </responsetype>
 * 
 * Notes:
 * 
 * There is only one responsetype occurence.
 * 
 * There can be many objecttypes (of the same type ideally) and this
 * 		data structure should support many of these. We should allow any
 * 		type of objecttype.
 * 
 * There can be many fields, though they must be unique. Duplicate
 * 		fields will overwrite existing entries.
 * 
 * @author pkilgo
 *
 */
public class ClaXMLSet {
	
	/** This will hold all of the objects */
	private ArrayList< HashMap<String, String> > mData = new ArrayList< HashMap<String, String> >();
	
	/** Stores the response that came from the server (e.g., "error", "list").*/
	private String mResponseType;
	
	/** Remembers the object type that we are storing */
	private String mObjectType;
	
	/** Boolean variable telling us whether or not the format was valid */
	private boolean mInvalid = false;
	
	/** Add an object to the data structure */
	public void addObject(HashMap<String, String> object) {
		mData.add(object);
	}
	
	/** @Return the object at position 'n' */
	public HashMap<String,String> getObject(int n) {
		return mData.get(n);
	}
	
	/**
	 * Access the ArrayList storing the data. Excellent use with SimpleAdapter!
	 * @return ArrayList holding all of the result rows.
	 */
	public ArrayList< HashMap<String,String> > getData() {
		return mData;
	}
	
	/**
	 * Returns a boolean value indicating whether or not this
	 * data set was properly formatted.
	 * @return the invalidity bit
	 */
	public boolean getInvalid() {
		return mInvalid;
	}
	
	/** @Return the object type */
	public String getObjectType() {
		return mObjectType;
	}
	
	/** @Return the response type */
	public String getResponseType() {
		return mResponseType;
	}
	
	/** @Return the data structure's size */
	public int getSize() {
		return mData.size();
	}
	
	/** Removes the object at position 'n' 
	 * @return Object which was removed.
	 */
	public HashMap<String, String> removeObjectAt(int n) {
		return mData.remove(n);
	}
	
	public void setInvalid(boolean b) {
		mInvalid = b;
	}
	
	/** Set the object type */
	public void setObjectType(String object) {
		mObjectType = object;
	}
	
	/** Set the response type */
	public void setResponseType(String response) {
		mResponseType = response;
	}
	
	/**
	 * Construct a string representation of ClaXMLSet
	 * @return a string representation
	 */
	public String toString() {
		String ret = "Response: " + mResponseType + "\n";
		ret += "Object: " + mObjectType + "\n";
		ret += "Invalid: " + mInvalid + "\n";
		ret += mData.toString();
		return ret;
	}
}
